﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using NativeWifi;
using WNADEP.DataCenter.NetworkDataSet;

namespace WNADEP.DataCenter.NetworkDataSet
{
    class WIFISSID : INotifyPropertyChanged
    {

        #region Field

        string _SSID = "null";
        int _wlanSignalQuality = 0;
        uint _Channel;
        int _RSSI;
        string _bssType;
        uint _signalQuality;
        bool _Connected;
        bool _isEmergent;

        public Wlan.Dot11AuthAlgorithm dot11DefaultAuthAlgorithm;
        public Wlan.Dot11CipherAlgorithm dot11DefaultCipherAlgorithm;
        public bool networkConnectable = true;
        public string wlanNotConnectableReason = "";
        public MacAddress macAddress = null;

        public WlanClient.WlanInterface wlanInterface = null;
        #endregion Field

        #region Binding Name

        public string SSID
        {
            get
            {
                return _SSID;
            }
            set
            {
                _SSID = value;
                Notify("SSID");
            }
        }

        public int WlanSignalQuality
        {
            get
            {
                return _wlanSignalQuality;
            }
            set
            {
                _wlanSignalQuality = value;
                Notify("wlanSignalQuality");
            }
        }

        public string Security
        {
            get
            {
                return WIFISSID.CreateSecurityString(dot11DefaultAuthAlgorithm, dot11DefaultCipherAlgorithm);
            }
            set
            {
                Notify("Security");
            }
        }

        public uint Channel
        {
            get
            {
                return _Channel;
            }
            set
            {
                _Channel = value;
                Notify("Channel");
            }
        }

        public int RSSI
        {
            get
            {
                return _RSSI;
            }
            set
            {
                _RSSI = value;
                Notify("RSSI");
            }
        }

        public string BssType
        {
            get
            {
                return _bssType;
            }
            set
            {
                _bssType = value;
                Notify("bssType");
            }
        }

        public uint SignalQuality
        {
            get
            {
                return _signalQuality;
            }
            set
            {
                _signalQuality = value;
                Notify("SignalQuality");
            }
        }

        public bool Connected
        {
            get
            {
                return _Connected;
            }
            set
            {
                _Connected = value;
                Notify("Connected");
            }
        }

        public string MacAddress
        {
            get
            {
                if (macAddress == null) return "null";
                return macAddress.ToString();
            }
            set
            {
                Notify("MacAddress");
            }
        }

        public bool IsEmergent
        {
            get
            {
                return _isEmergent;
            }
            set
            {
                _isEmergent = value;
                Notify("IsEmergent");
            }
        }

        #endregion Binding Name

        #region Event
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion Event

        #region Constructors

        public WIFISSID()
        {
            Connected = false;
        }

        public WIFISSID(byte[] _macAddress)
            : this()
        {
            macAddress = new MacAddress(_macAddress);
        }
        #endregion Constructrors

        #region Private Methods
        private void Notify(string Name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(Name));
        }
        #endregion Private Methods

        #region Pulic Methods
        public static string GetStringForSSID(Wlan.Dot11Ssid ssid)
        {
            return Encoding.UTF8.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
        }

        public static string StringToHex(string str)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.Default.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(Convert.ToString(byStr[i], 16));
            }

            return (sb.ToString().ToUpper());
        }

        public static string CreateSecurityString(Wlan.Dot11AuthAlgorithm authentication,
            Wlan.Dot11CipherAlgorithm encryption)
        {
            String text = authentication + "-" + encryption;
            text = text.Replace("RSNA", "WPA2");
            text = text.Replace("_PSK-CCMP", "-Personal");
            text = text.Replace("_PSK-TKIP", "-Personal");
            text = text.Replace("CCMP", "Enterprise");
            text = text.Replace("IEEE80211_", "");
            text = text.Replace("None", "");
            text = text.Replace("Open", "");
            text = text.Replace("WEP40", "WEP");
            text = text.Replace("WEP104", "WEP");
            text = text.Trim(new[] { '-' });
            if (null == text || text.Equals(String.Empty))
            {
                text = "Open";
            }
            return text;
        }

        #endregion Pulic Methods
    }

}
